package com.ruoyi.develop.goodsInvoiceApplyDetail.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.develop.projectBaseInfo.domain.ProjectBaseInfo;
import com.ruoyi.develop.projectBaseInfo.service.IProjectBaseInfoService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.goodsInvoiceApplyDetail.domain.GoodsInvoiceApplyDetail;
import com.ruoyi.develop.goodsInvoiceApplyDetail.service.IGoodsInvoiceApplyDetailService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 发票申请明细Controller
 * @author qh
 * @date 2025-04-26
 */
@RestController
@RequestMapping("/goodsInvoiceApplyDetail/goodsInvoiceApplyDetail")
public class GoodsInvoiceApplyDetailController extends BaseController {
    @Autowired
    private IGoodsInvoiceApplyDetailService goodsInvoiceApplyDetailService;
    @Autowired
    private IProjectBaseInfoService projectBaseInfoService;


    /**
     * 查询发票申请明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsInvoiceApplyDetail:query')")
    @GetMapping("/list")
    public TableDataInfo list(GoodsInvoiceApplyDetail entity) {
        startPage();
        List<GoodsInvoiceApplyDetail> list = goodsInvoiceApplyDetailService.selectList(entity);
        return getDataTable(list);
    }

    /**
     * 导出发票申请明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsInvoiceApplyDetail:export')")
    @Log(title = "发票申请明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GoodsInvoiceApplyDetail entity) {
        List<GoodsInvoiceApplyDetail> list = goodsInvoiceApplyDetailService.selectList(entity);
        ExcelUtil<GoodsInvoiceApplyDetail> util = new ExcelUtil<GoodsInvoiceApplyDetail>(GoodsInvoiceApplyDetail. class);
        util.exportExcel(response, list, "发票申请明细数据");
    }

    /**
     * 获取发票申请明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsInvoiceApplyDetail:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(goodsInvoiceApplyDetailService.selectById(id));
    }

    /**
     * 新增发票申请明细
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsInvoiceApplyDetail:add')")
    @Log(title = "发票申请明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody GoodsInvoiceApplyDetail entity) {
        return toAjax(goodsInvoiceApplyDetailService.insert(entity));
    }

    /**
     * 修改发票申请明细
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsInvoiceApplyDetail:edit')")
    @Log(title = "发票申请明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody GoodsInvoiceApplyDetail entity) {
        return toAjax(goodsInvoiceApplyDetailService.update(entity));
    }

    /**
     * 删除发票申请明细
     */
    @PreAuthorize("@ss.hasPermi('develop:GoodsInvoiceApplyDetail:remove')")
    @Log(title = "发票申请明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        for (String id : ids) {
            GoodsInvoiceApplyDetail detail =  goodsInvoiceApplyDetailService.selectById(id);
            // 更新订单上的 项目金额
            ProjectBaseInfo projectBaseInfo = projectBaseInfoService.selectById(detail.getProjectId());
            if(projectBaseInfo != null){
                projectBaseInfo.setTaxRemain(projectBaseInfo.getTaxRemain().add(detail.getAmount()));
                projectBaseInfoService.update(projectBaseInfo);
            }
        }
        return toAjax(goodsInvoiceApplyDetailService.deleteByIds(ids));
    }
}
